/**
 * admin 后台js
 *
 */
const BKM = 1024;

//字符串为空
function isNull(str) {
    if (str == null || str == "" || str.length < 1)
        return true;
    else
        return false;
}

/*检查输入框是否为空，并输出错误提示*/
function checkEmpty(data, holder, msg) {
    if (isNull(data)) {
        $(holder).html(msg);
        return false;
    } else {
        $(holder).html('');
        return true;
    }
}

//获取textarea字符长度，中文单字2字符，英文1字符
function getblen(str) {
    var l = str.length;
    var blen = 0;

    for (i = 0; i < l; i++) {
        if ((str.charCodeAt(i) & 0xff00) != 0) {
            blen++;
        }
        blen++;
    }

    return blen;
}

//textarea 字数提示
function textarealength(obj, toplength) {
    var note = $(obj).val();
    var blen = getblen(note);

    if (blen <= toplength) {
        $(".textarea-length").html(blen);
    } else {
        $(".textarea-length").css('color', 'red');
        $(".textarea-length").html(blen);
    }
}

// 时间戳转日期时间格式字符串 秒级时间戳
// 1. 使用toLocaleString() 时间是上下午12制的
function getLocalTime(uTime) { 
    return new Date(parseInt(uTime) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " "); 
}
// 2. 自己拼 Y-m-d H:i:s 格式
function formatDate(uTime) {
    if (uTime.toString().length > 11) {
        var dateTime =new Date(parseInt(uTime));
    } else {
        var dateTime =new Date((parseInt(uTime) * 1000));
    }
    var y=dateTime.getFullYear(); 
    var mon=addZero(dateTime.getMonth()+1); 
    var d=addZero(dateTime.getDate());
    var h=addZero(dateTime.getHours());
    var m=addZero(dateTime.getMinutes()); 
    var s=addZero(dateTime.getSeconds());
    return y + '-' + mon + '-' + d + "&nbsp;&nbsp;&nbsp;" + h + ":" + m + ":" + s;
}

// add a zero in front of numbers<10
function addZero(num) {
    num = num < 10 ? "0" + num : num;
    return num;
}

// 获取当前系统时间
function time() {
    var today = new Date()
    var y = today.getFullYear();
    var mon = addZero(today.getMonth() + 1);
    var d = addZero(today.getDate());
    var h = addZero(today.getHours());
    var m = addZero(today.getMinutes());
    var s = addZero(today.getSeconds());
    return y + '-' + mon + '-' + d + "&nbsp;&nbsp;&nbsp;" + h + ":" + m + ":" + s;
}


//扩展jquery的格式化方法, serializeArray() 遍历出对象结果
$.fn.parseForm=function(){
    var serializeObj={};
    var array=this.serializeArray();
    var str=this.serialize();
    $(array).each(function(){
        if(serializeObj[this.name]){
            if($.isArray(serializeObj[this.name])){
                serializeObj[this.name].push(this.value);
            }else{
                serializeObj[this.name]=[serializeObj[this.name],this.value];
            }
        }else{
            serializeObj[this.name]=this.value; 
        }
    });
    return serializeObj;
};

// 格式化文件大小输出
function sizeBKM(filesize) {
    if (filesize>BKM) {
        filesize = (filesize/BKM).toFixed(2);
    
        if (filesize>BKM) {
            filesize = (filesize/BKM).toFixed(2);
        
            filesize +="M";
        } else {
            filesize +="K";
        }
    } else {
        filesize +="B";
    }
    return filesize;
}

/* 获取uuid*/
function getUUID () {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
        return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
    })
}

/* 删除列表条目 */
function del(obj, id) {
    layer.confirm('确认要删除吗？', function (index) {
        $.post('del', {
            'id': id
        }, function (data) {
            if (data.code == 2) {
                $(obj).parents("tr").remove();
                layer.msg(data.msg, {
                    icon: 1,
                    time: 1000
                });
            } else {
                layer.msg(data.msg, {
                    icon: 5,
                    time: 2000
                });
                return false;
            }
        }, 'json');
    });
}

/* 删除列表多选条目 */
function del_all() {
    var checkbox = $('.text-c input[name="checkbox[]"]');
    var ids = new Array();
    checkbox.each(function (x) {
        if (this.checked)
            ids.push(this.value);
    })

    var length = ids.length;

    if (length == 0) {
        layer.msg('请选择要删除的选项', {
            icon: 5,
            time: 1000
        });
        return false;
    }

    layer.confirm('确认要删除吗？', function (index) {
        $.post('del', {
            'id': ids
        }, function (data) {
            if (data.code == 2) {
                checkbox.each(function (x) {
                    if (this.checked)
                        $(this).parents("tr").remove();
                })
                layer.msg(data.msg, {
                    icon: 1,
                    time: 1000
                });
            } else {
                layer.msg(data.msg, {
                    icon: 5,
                    time: 2000
                });
                return false;
            }
        }, 'json');
    });
}

/* ajax 改变 status 值 */
function status(action, obj, id) {
    var value = 2;
    var title = '启用';
    var status = '停用';
    var color = 'danger';
    if (action == 'start') {
        value = 1;
        action = 'stop';
        title = '停用';
        status = '启用';
        color = 'success';
    } else {
        action = 'start';
    }

    var text = "<a href=\"javascript:void(0);\" title='" + title + "' onClick=\"status('" + action + "',this," + id + ")\">";
    text += '<span class="label label-' + color + ' radius" >' + status + '</span>';
    text += '</a>';

    $.post('status', {
        'id': id,
        'value': value
    }, function (data) {
        if (data.code == 2) {
            $(obj).after(text);
            $(obj).remove();
            layer.msg(data.msg, {
                icon: 6,
                time: 1000
            });
        } else {
            layer.msg(data.msg, {
                icon: 5,
                time: 1000
            });
        }
    }, 'json');
}